<?php

/*
 * 扫码进门
 */
namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class ScanCodeController extends  Controller{

    public function ScanCode(Request $request)
    {
        if ($request->isMethod('post')) {
            $Array = (object)array();
            $village_id = $request->input('village_id');
            $id = $request->input('visitors_id');
            $visitors_toke = $request->input('visitors_toke');
            $select = DB::table('visitors')->where('visitors_id', $id)->first();
            $user = DB::table('user')
                ->leftjoin('village', 'user.user_village', '=', 'village.village_id')
                ->where('user_id', $select->user_visitors)
                ->first();
            if ($village_id == $user->village_id) {
                $time = date('Y-m-d H:i:s', time());
                if ($select->visitors_time < $time) {
                    return json::json('failure', '对不起你的二维码过了有效时间', $Array);
                }elseif ($select ->visitors_state ==1){
                    return json::json('failure', '对不起您的二维码不能重复', $Array);
                } else {
                    $data = DB::table('visitors')
                        ->where('visitors_id', $id)
                        ->where('visitors_toke', $visitors_toke)

                        ->update([
                            'visitors_state' => 1
                        ]);
                    if ($data == true) {
                        return json::json('success', '扫码成功', $Array);
                    } else {
                        return json::json('failure', '由于网络或者不正当原因失败', $Array);
                    }
                }

            } else {
                $Array = (object)array();
                return json::json('failure', '对不起，您的二维码在小区无法使用', $Array);
            }
        }
    }

}
